package com.example.demoexample.utils;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RxJavaUtil {
    private static Observable.Transformer mTransformer = new Observable.Transformer() {
        @Override
        public Object call(Object o) {
            return ((Observable) o).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };

    public static Observable<Integer> countDown(int countTime) {
        return Observable.interval(1, TimeUnit.SECONDS)
                .take(countTime + 1)
                .map(curTime -> countTime - curTime.intValue())
                .compose(applySchedulers());
    }

    public static <T> Observable.Transformer<T, T> applySchedulers() {
        return (Observable.Transformer<T, T>) mTransformer;
    }
}
